/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mrp.productionPlanning.session;

import crm.salesOrderProcessing.entity.DeliveryOrderEntity;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import mrp.productionPlanning.util.exception.MRPException;
import crm.salesOrderProcessing.entity.SalesOrderEntity;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import javax.ejb.EJB;
import mrp.productionPlanning.entity.DailyDetailEntity;
import mrp.productionPlanning.util.exception.ATPFailedException;

/**
 *
 * @author ReinForceII
 */
@Stateless
@LocalBean
public class MRPATPSessionBean {

    @EJB
    MonthlyDetailSessionBean mdsb = new MonthlyDetailSessionBean();
    @EJB
    DailyDetailSessionBean ddsb = new DailyDetailSessionBean();
    @EJB
    WeeklyDetailSessionBean wdsb = new WeeklyDetailSessionBean();
    @EJB
    processSalesOrderSessionBean psosb = new processSalesOrderSessionBean();
    
    public void aTPCheck(SalesOrderEntity s) throws MRPException{
        return;
    }
    
    
//    public void aTPCheck(SalesOrderEntity salesOrder) throws MRPException {
//        boolean flag = false;
//        Calendar daysLater = Calendar.getInstance();
//        daysLater.add(Calendar.DAY_OF_MONTH, 15);
//        LineItemEntity failedItem = null;
//        
//        
//        for (LineItemEntity item : salesOrder.getSalesOrderLineItems())
//        {
//            
//            if (item.getDeliveryDate().before(daysLater.getTime())){
//                flag = true;
//                failedItem = item;
//                break;
//            }
//        }
//        if (flag)
//            throw new ATPFailedException(failedItem.getProduct().getProductName() + "cannnot be delivered on" + failedItem.getDeliveryDate().toString());
//        return;
//    }
    
    public void aTPCheck(List<DeliveryOrderEntity> doeList) throws MRPException{
        boolean flag = true;
        Calendar cal = new GregorianCalendar();
        Date day = null;
        int leadTime = 0;
        String msg = null;
        StringBuilder builder = new StringBuilder("Id of failed Delivery Order:    ");
        List<DeliveryOrderEntity> failedList = new ArrayList<DeliveryOrderEntity>();
        List<DailyDetailEntity> period  = new ArrayList<DailyDetailEntity>();
        for (DeliveryOrderEntity doe:doeList){
               day = doe.getDeliveryDate();
               period = ddsb.getPeriod(day, day);
               leadTime = psosb.leadTimeEstimation(doe);
               if (leadTime > period.size()){
                   flag = false;
                   failedList.add(doe);
               }
        }
        if(!flag){
            for (DeliveryOrderEntity doe:failedList){
                builder.append(doe.getId());
                builder.append("    ");
            }
            throw new ATPFailedException(msg);
        }
    }
}
